package com.hx.product.service.impl;

import com.hx.common.utils.DateUtils;
import com.hx.common.utils.StringUtils;
import com.hx.product.domain.AttrValue;
import com.hx.product.domain.Goods;
import com.hx.product.domain.Images;
import com.hx.product.mapper.GoodsMapper;
import com.hx.product.service.IGoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品Service业务层处理
 * 
 * @author Chicken Brother Team
 * @date 2023-05-25
 */
@Service
public class GoodsServiceImpl implements IGoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    /**
     * 查询商品
     * 
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public Goods selectGoodsById(Integer id)
    {
        return goodsMapper.selectGoodsById(id);
    }

    /**
     * 查询商品列表
     * 
     * @param goods 商品
     * @return 商品
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods)
    {
        return goodsMapper.selectGoodsList(goods);
    }

    /**
     * 新增商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGoods(Goods goods)
    {
        goods.setCreateTime(DateUtils.getNowDate());
        int rows = goodsMapper.insertGoods(goods);
        insertAttrValue(goods);
        insertImages(goods);
        return rows;
    }

    /**
     * 修改商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGoods(Goods goods)
    {
        goods.setUpdateTime(DateUtils.getNowDate());
        goodsMapper.deleteAttrValueByGoodsId(goods.getId());
        insertAttrValue(goods);
        goodsMapper.deleteImagesByGoodsId(goods.getId());
        insertImages(goods);
        return goodsMapper.updateGoods(goods);
    }

    /**
     * 批量删除商品
     * 
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGoodsByIds(Integer[] ids)
    {
        goodsMapper.deleteAttrValueByGoodsIds(ids);
        goodsMapper.deleteImagesByGoodsIds(ids);
        return goodsMapper.deleteGoodsByIds(ids);
    }

    /**
     * 删除商品信息
     * 
     * @param id 商品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGoodsById(Integer id)
    {
        goodsMapper.deleteAttrValueByGoodsId(id);
        goodsMapper.deleteGoodsById(id);
        return goodsMapper.deleteGoodsById(id);
    }

    /**
     * 新增商品属性与属性值关联信息
     * 
     * @param goods 商品对象
     */
    public void insertAttrValue(Goods goods)
    {
        List<AttrValue> attrValueList = goods.getAttrValueList();
        Integer id = goods.getId();
        if (StringUtils.isNotNull(attrValueList))
        {
            List<AttrValue> list = new ArrayList<AttrValue>();
            for (AttrValue attrValue : attrValueList)
            {
                attrValue.setGoodsId(id);
                list.add(attrValue);
            }
            if (list.size() > 0)
            {
                goodsMapper.batchAttrValue(list);
            }
        }
    }

    public void insertImages(Goods goods)
    {
        List<Images> imagesList = goods.getImageList();
        Integer id = goods.getId();
        if (StringUtils.isNotNull(imagesList))
        {
            List<Images> list = new ArrayList<>();
            for (Images images : imagesList)
            {
                images.setCreateTime(new Date());
                images.setGoodsId((long)id);
                list.add(images);
            }
            if (list.size() > 0)
            {
                goodsMapper.batchImages(list);
            }
        }
    }
}
